package com.ly.chatsimple.tcp.task

import com.ly.chatsimple.tcp.TcpClient
import com.ly.data.MessageProtobuf
import com.ly.data.MessageProtobuf.Msg
import com.ly.nethub.task.BaseTask
import com.ly.nethub.utils.LogUtils

class ChatTask private constructor(private val msg: Msg) : BaseTask() {
    override fun buildRequest(): Any {
        LogUtils.d("ChatTask taskId:$taskId")
        return MessageProtobuf.TaskProto.newBuilder()
            .setSeq(taskId)
            .setType(MessageProtobuf.TaskType.Chat)
            .setData(msg.toByteString())
            .setFrom(TcpClient.ip)
            .setVersion("v1.0")
            .build()
    }

    override fun isValidResponse(msg: Any?): Any? {
        if (msg == null) return null
        if (msg !is MessageProtobuf.TaskProto) {
            return null
        }
        if (msg.seq == taskId) return msg
        return null
    }


    companion object {
        fun create(msg: Msg): ChatTask = ChatTask(msg)
    }
}